//
//  OMGGroupViewController.m
//  A_project2.0
//
//  Created by Tiny on 16/3/7.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "OMGGroupViewController.h"
#import "OMGCollectionViewCell.h"
#import "OMGGroupModel.h"
#import "OMGGroupModeltwo.h"
#import "OMGGroupCollectionReusableView.h"
#import "OMGGroupSecViewController.h"

#import <AFNetworking.h>
#import <UIImageView+AFNetworking.h>

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface OMGGroupViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSMutableArray *sectionArr; //分区数组
@property (nonatomic, strong)NSMutableArray *titleArr; //分区头上的穷游7大洲
@property (nonatomic, strong)NSMutableArray *cidArr;
@end

@implementation OMGGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"社区";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.cidArr = [NSMutableArray array];
    [self dataHandler];
    [self createCollectionView];
    
    //数组
    
    
    
    self.titleArr = [@[@"穷游亚洲",@"穷游欧洲",@"穷游美洲",@"穷游大洋洲",@"穷游非洲",@"穷游兴趣小组",@"线下线上",@"后院"] mutableCopy];
      
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //cell大小
    layout.itemSize = CGSizeMake((WIDTH - 40) / 2, 50);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(10, 15, 5, 5);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[OMGCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //头部
    [_collectionView registerClass:[OMGGroupCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    layout.headerReferenceSize = CGSizeMake(100, 50);
    
}

- (void)dataHandler
{
    NSString *str = @"http://open.qyer.com/qyer/bbs/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&v=1";
    [QPNetWorkingTool getNetWorkingTool:str dic:nil success:^(id result) {
        // 数据处理
        NSArray *arr = result[@"data"][@"forum_list"];
        self.sectionArr = [OMGGroupModeltwo modelHanderWithArray:arr];
        for (OMGGroupModeltwo *m in _sectionArr) {
            NSMutableArray *arrTemp = [NSMutableArray array];
//            self.cidArr = [NSMutableArray array];
            for (NSDictionary *dic2 in m.group) {
                [arrTemp addObject:dic2[@"id"]];
                
            }
            [_cidArr addObject:arrTemp];
        }
        [_collectionView reloadData];
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - collectionView协议
//头部
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _sectionArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    OMGGroupCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    header.headerLabel.text = _titleArr[indexPath.section];
    return header;
}

//cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[_sectionArr[section] group] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    OMGGroupModeltwo *p = _sectionArr[indexPath.section];
    NSArray *arr = p.group;
    self.dataArr = [OMGGroupModel modelHanderWithArray:arr];
    
    OMGGroupModel *m = _dataArr[indexPath.item];
    [cell.photoImg setImageWithURL:[NSURL URLWithString:m.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    cell.nameLabel.text = m.name;
    cell.total_threadLabel.text = [NSString stringWithFormat:@"%@个帖子", m.total_threads];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *str = _cidArr[indexPath.section][indexPath.item];
    OMGGroupSecViewController *secVC = [[OMGGroupSecViewController alloc]init];
    secVC.str = [str stringValue];
    [self.navigationController pushViewController:secVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
